package com.qlm.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.qlm.common.Define;
import com.qlm.service.IScoreService;
import com.qlm.tools.Common;
import com.qlm.view.ResultMessage;

/**
 * 积分设置接口实现类
 * 
 * @author raoy
 *
 */
public class ScoreServiceImpl implements IScoreService {
	
	protected final static Logger logger = LoggerFactory.getLogger(ScoreServiceImpl.class); 	

	@Override
	public ResultMessage getScoreConfig() {
		Record record = getScore();
		if(record == null){
			record = new Record().set("id", "").set("score_start", "").set("score_end", "").
					set("default_score", "").set("add_score", "").set("days", "").set("attention_score", "").set("share_score", "");
		}
		return ResultMessage.setAllMessage(Define.RM_SUCCESS, "读取积分配置成功",record);
	}

	@Override
	public ResultMessage saveScoreConfig(Integer score_start,Integer score_end,Integer id,Integer default_score,Integer add_score,Integer days,Integer attention_score,Integer share_score) {
		if(Common.isNullOrEmpty(score_start) || Common.isNullOrEmpty(score_end)  || Common.isNullOrEmpty(attention_score) || Common.isNullOrEmpty(share_score)
				|| Common.isNullOrEmpty(default_score) || Common.isNullOrEmpty(add_score) || Common.isNullOrEmpty(days)){
			return ResultMessage.setAllMessage(Define.RM_FAIL, "网络连接错误");
		}		
		Record record = getScore();
		if(record == null){
			Db.save("t_scoreconfig", new Record().set("score_start", score_start).set("score_end", score_end).set("default_score", default_score).set("add_score", add_score).set("attention_score", attention_score).set("share_score", share_score).set("days", days));
		}else{
			Db.update("t_scoreconfig", new Record().set("score_start", score_start).set("score_end", score_end).set("default_score", default_score).set("add_score", add_score).set("days", days).set("attention_score", attention_score).set("share_score", share_score).set("id", id));
		}
		callCache("cache/updateScore");
		return ResultMessage.setAllMessage(Define.RM_SUCCESS, "积分配置保存成功");
	}
	
	private static Record getScore(){
		return Db.findFirst("select id,score_start,score_end,default_score,add_score,days,attention_score,share_score from t_scoreconfig");
	}
	
	/**
	 * 发送请求给cache更新
	 * @param type
	 */
	public static void callCache(String type){
			new Thread(new Runnable() {
				public void run() {
					String cacheUrl = PropKit.get("cacheUrl");
					String[] urlArr = cacheUrl.split(",");
					try{						
						for(int i=0;i<urlArr.length;i++){
							if(Common.isEmptyString(urlArr[i])){
								HttpKit.get(urlArr[i] + type);
								logger.info(urlArr[i]+type+"请求发送成功");
							}							
						}
					}catch(Exception e){
						logger.error(e.getMessage(), e);
					}	
				}
			}).start();
	}
}
